package cn.lkx.servicevod.service.impl;

import cn.lkx.servicebase.exceptionhandle.MyException;
import cn.lkx.servicevod.service.VodService;
import cn.lkx.servicevod.utils.ConstantVodUtils;
import cn.lkx.servicevod.utils.InitVodClient;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    //删除单个视频
    @Override
    public void removeVideo(String videoId) {
        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建 删除视频的 request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            //调用方法  删除
            client.getAcsResponse(request);

        } catch (ClientException e) {
            throw new MyException(20001,"视频删除失败");
        }

    }

    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID;
            String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET;
            String fileName = file.getOriginalFilename(); // fileName ：文件原名称
            //截取 . 之前的文件名 如：abcd.mp4 -> abcd
            String title = fileName.substring(0,fileName.lastIndexOf("."));//title ：上传之后显示的名称
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl(); //创建上传对象
            UploadStreamResponse response = uploader.uploadStream(request); //request传入
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId(); //视频id 获取
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();

            }
            return videoId;
        }catch (IOException e){
            e.printStackTrace();
            throw new MyException(20001,"视频上传失败！");
        }

    }
    //根据 视频id集合 批量删除视频
    @Override
    public void removeVideoList(List<String> videoIdList) {
        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建 删除视频的 request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //需要将videoIdList 转成1,22,33 形式
            String videoIds = String.join(",",videoIdList);
            request.setVideoIds(videoIds);//传入多个视频id
            //调用方法  删除
            client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new MyException(20001,"视频批量删除失败");
        }
    }
}
